﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sispe.Entities;
using Sispe.BussinesLogic;
using Microsoft.Practices.Unity;

namespace Sispe.UI
{
    public partial class FrmMttoSanciones : Form
    {
        [Dependency]
        public ISancionFaltasBL sanFaltaBL { get; set; }

        [Dependency]
        public ITipoSancionBL tipSancion { get; set; }

        public FrmMttoSanciones()
        {
            InitializeComponent();
            this.BuildUp();

            tIPOSANCIONBindingSource.DataSource = tipSancion.GetAllFromTipoSancion();
        }

        bool isNew;

        public void NuevoSancion()
        {
            isNew = true;
            sANCION_FALTABindingSource.AddNew();
        }

        public SANCION_FALTA _sancionFalta { get; set; }
        public CONTRATO _currentContrato { get; set; }
        
        public void EditarSancion()
        {
            isNew = false;
            sANCION_FALTABindingSource.DataSource = _sancionFalta;
        }

        private void FrmMttoSanciones_Load(object sender, EventArgs e)
        {

        }

        private void comboBox1_Click(object sender, EventArgs e)
        {
            tIPOSANCIONBindingSource.DataSource = tipSancion.GetAllFromTipoSancion();
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (cbTipSancion.SelectedValue != null)
            {
                idTipoSancionTextBox.Text = cbTipSancion.ToString();
            }
        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            var sanFalta = sANCION_FALTABindingSource.Current as SANCION_FALTA;
            var msg = "ERROR";

            if (sanFalta != null)
            {
                if (isNew == true)
                {
                    try
                    {
                        sanFalta.Motivo = motivoTextBox.Text.ToUpper();
                        sanFalta.IdContrato = _currentContrato.IdContrato;
                        sanFalta.FechaInicio = fechaInicioDateTimePicker.Value;
                        sanFalta.FechaTermino = fechaTerminoDateTimePicker.Value;
                        sanFaltaBL.AddSancionFalta(sanFalta);
                        MessageBox.Show("Registro Correcto", "Registro",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                if (isNew == false)
                {
                    try
                    {
                        sanFalta.Motivo = motivoTextBox.Text.ToUpper();
                        sanFaltaBL.UpdateSancionFalta(sanFalta);
                        MessageBox.Show("Modificación Correcta", "Registro",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void motivoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var coma = Convert.ToString(",");
            var punto = Convert.ToString(".");
            var parentesis = Convert.ToString("()");
            var puntoComa = Convert.ToString(";");
            var slash = Convert.ToString("/");

            string cadena = coma + punto + parentesis + puntoComa + slash + (char)8;

            if (Char.IsLetter(e.KeyChar) || cadena.Contains(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
    }
}
